<template>
  <div class="setting-drawer" ref="settingDrawer">
    <Drawer
      width="300"
      placement="right"
      @on-visible-change="onChange"
      :closable="false"
      :value="visible"
    >
      <div class="setting-drawer-index-content">
        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">整体风格设置</h3>

          <div class="setting-drawer-index-blockChecbox">
            <Tooltip>
              <template slot="content">
                暗色菜单风格
              </template>
              <div class="setting-drawer-index-item" @click="handleMenuTheme('dark')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg" alt="dark">
                <div class="setting-drawer-index-selectIcon" v-if="navTheme === 'dark'">
                  <Icon type="md-checkmark"/>
                </div>
              </div>
            </Tooltip>

            <Tooltip>
              <template slot="content">
                亮色菜单风格
              </template>
              <div class="setting-drawer-index-item" @click="handleMenuTheme('light')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg" alt="light">
                <div class="setting-drawer-index-selectIcon" v-if="navTheme !== 'dark'">
                  <Icon type="md-checkmark"/>
                </div>
              </div>
            </Tooltip>
          </div>
        </div>

        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">主题色</h3>

          <div style="height: 20px">
            <Tooltip v-for="(item, index) in colorList" :key="index">
              <template slot="content">
                {{ item.key }}
              </template>
              <Tag
                class="setting-drawer-theme-color-colorBlock"
                :color="item.color"
                @click.native="changeColor(item.color)">
                <Icon type="md-checkmark" v-if="item.color === primaryColor"></Icon>
              </Tag>
            </Tooltip>
          </div>
        </div>
        <Divider/>

        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">导航模式</h3>

          <div class="setting-drawer-index-blockChecbox">
            <Tooltip>
              <template slot="content">
                侧边栏导航
              </template>
              <div class="setting-drawer-index-item" @click="handleLayout('sidemenu')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg" alt="sidemenu">
                <div class="setting-drawer-index-selectIcon" v-if="layoutMode === 'sidemenu'">
                  <Icon type="md-checkmark"/>
                </div>
              </div>
            </Tooltip>

            <Tooltip>
              <template slot="content">
                顶部栏导航
              </template>
              <div class="setting-drawer-index-item" @click="handleLayout('topmenu')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg" alt="topmenu">
                <div class="setting-drawer-index-selectIcon" v-if="layoutMode !== 'sidemenu'">
                  <Icon type="md-checkmark"/>
                </div>
              </div>
            </Tooltip>
          </div>
          <div :style="{ marginTop: '24px' }">
            <CellGroup>
              <Cell title="内容区域宽度">
                <template slot="extra">
                  <Tooltip transfer placement="left" content="该设定仅 [顶部栏导航] 时有效">
                    <Select
                      transfer
                      size="small"
                      style="width: 80px;"
                      :value="contentWidth"
                      @on-change="handleContentWidthChange">
                      <Option value="Fixed">固定</Option>
                      <Option value="Fluid" v-if="layoutMode !== 'sidemenu'">流式</Option>
                    </Select>
                  </Tooltip>
                </template>
              </Cell>
              <Cell title="固定 Header">
                <template slot="extra">
                  <i-switch size="small" :value="fixedHeader" @on-change="handleFixedHeader"/>
                </template>
              </Cell>
              <Cell :disabled="!fixedHeader">
                <template slot="">
                  <Tooltip placement="left" transfer content="固定 Header 时可配置">
                    <div>下滑时隐藏 Header</div>
                  </Tooltip>
                </template>
                <template slot="extra">
                  <i-switch
                    size="small"
                    :disabled="!fixedHeader"
                    :value="autoHideHeader"
                    @on-change="handleFixedHeaderHidden"/>
                </template>
              </Cell>
              <Cell title="固定侧边菜单" :disabled="layoutMode === 'topmenu'">
                <template slot="extra">
                  <i-switch
                    size="small"
                    :disabled="(layoutMode === 'topmenu')"
                    :value="fixSiderbar"
                    @on-change="handleFixSiderbar"/>
                </template>
              </Cell>
            </CellGroup>
          </div>
        </div>
        <Divider/>

        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">其他设置</h3>
          <div>
            <CellGroup>
              <Cell title="色弱模式">
                <template slot="extra">
                  <i-switch size="small" :value="colorWeak" @on-change="onColorWeak"/>
                </template>
              </Cell>
              <Cell title="多页签模式">
                <template slot="extra">
                  <i-switch size="small" :value="multiTab" @on-change="onMultiTab"/>
                </template>
              </Cell>
            </CellGroup>
          </div>
        </div>
        <Divider/>
        <div :style="{ marginBottom: '24px' }">
          <Button
            @click="doCopy"
            icon="copy"
            block
          >拷贝设置
          </Button>
          <Alert type="warning" :style="{ marginTop: '24px' }">
            配置栏只在开发环境用于预览，生产环境不会展现，请手动修改配置文件
          </Alert>
        </div>
      </div>
    </Drawer>
    <div class="setting-drawer-index-handle" :style="{right:right}" @click="toggle">
      <Icon type="md-settings" v-if="!visible"/>
      <Icon type="md-close" v-else/>
    </div>
  </div>
</template>

<script>
import SettingItem from './SettingItem'
import config from '@/config'
import { updateTheme, updateColorWeak, colorList } from './settingConfig'
import { mixin, mixinDevice } from '@/utils/mixin'

export default {
  components: {
    SettingItem
  },
  mixins: [mixin, mixinDevice],
  data () {
    return {
      visible: false,
      colorList,
      right: '0'
    }
  },
  watch: {},
  mounted () {
    const vm = this
    setTimeout(() => {
      vm.visible = false
    }, 16)
    updateTheme(this.primaryColor)
    if (this.colorWeak !== config.colorWeak) {
      updateColorWeak(this.colorWeak)
    }
  },
  methods: {
    onChange (visible) {
      this.visible = visible
      this.right = visible ? '300px' : '0'
    },
    toggle () {
      this.visible = !this.visible
    },
    onColorWeak (checked) {
      this.$store.dispatch('ToggleWeak', checked)
      updateColorWeak(checked)
    },
    onMultiTab (checked) {
      this.$store.dispatch('ToggleMultiTab', checked)
    },
    handleMenuTheme (theme) {
      this.$store.dispatch('ToggleTheme', theme)
    },
    doCopy () {
      // get current settings from mixin or this.$store.state.app, pay attention to the property name
      const text = `export default {
  primaryColor: '${this.primaryColor}', // 主色彩
  navTheme: '${this.navTheme}', // 导航主题色
  layout: '${this.layoutMode}', // 导航布局: sidemenu or topmenu
  contentWidth: '${this.contentWidth}', // 内容区域宽度 : Fluid or Fixed, only works when layout is topmenu
  fixedHeader: ${this.fixedHeader}, // 固定 Header
  fixSiderbar: ${this.fixSiderbar}, // 固定侧边菜单
  autoHideHeader: ${this.autoHideHeader}, //  下滑时隐藏 Header
  colorWeak: ${this.colorWeak},
  multiTab: ${this.multiTab},
  production: process.env.NODE_ENV === 'production' && process.env.VUE_APP_PREVIEW !== 'true',
  // vue-ls options
  storageOptions: {
    namespace: 'pro__',
    name: 'ls',
    storage: 'local',
  }
}`
      this.$copyText(text).then(message => {
        console.log('copy', message)
        this.$Message.success('复制完毕')
      }).catch((e) => {
        console.log('copy.err', e)
        this.$Message.error('复制失败')
      })
    },
    handleLayout (mode) {
      this.$store.dispatch('ToggleLayoutMode', mode)
      // 因为顶部菜单不能固定左侧菜单栏，所以强制关闭
      this.handleFixSiderbar(false)
    },
    handleContentWidthChange (type) {
      this.$store.dispatch('ToggleContentWidth', type)
    },
    changeColor (color) {
      if (this.primaryColor !== color) {
        this.$store.dispatch('ToggleColor', color)
        updateTheme(color)
      }
    },
    handleFixedHeader (fixed) {
      this.$store.dispatch('ToggleFixedHeader', fixed)
    },
    handleFixedHeaderHidden (autoHidden) {
      this.$store.dispatch('ToggleFixedHeaderHidden', autoHidden)
    },
    handleFixSiderbar (fixed) {
      if (this.layoutMode === 'topmenu') {
        this.$store.dispatch('ToggleFixSiderbar', false)
        return
      }
      this.$store.dispatch('ToggleFixSiderbar', fixed)
    }
  }
}
</script>

<style lang="less" scoped>

  .setting-drawer-index-content {

    .setting-drawer-index-blockChecbox {
      display: flex;

      .setting-drawer-index-item {
        margin-right: 16px;
        position: relative;
        border-radius: 4px;
        cursor: pointer;

        img {
          width: 48px;
        }

        .setting-drawer-index-selectIcon {
          position: absolute;
          top: 0;
          right: 0;
          width: 100%;
          padding-top: 15px;
          padding-left: 24px;
          height: 100%;
          color: #2d8cf0;
          font-size: 14px;
          font-weight: 700;
        }
      }
    }

    .setting-drawer-theme-color-colorBlock {
      width: 22px;
      height: 22px;
      border-radius: 2px;
      float: left;
      cursor: pointer;
      margin-right: 8px;
      padding: 0px;
      text-align: center;
      color: #fff;
      font-weight: 700;

      i {
        font-size: 14px;
      }
    }
  }

  .setting-drawer-index-handle {
    position: absolute;
    top: 240px;
    background: #2d8cf0;
    width: 48px;
    height: 48px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    pointer-events: auto;
    z-index: 1001;
    text-align: center;
    font-size: 16px;
    border-radius: 4px 0 0 4px;

    i {
      color: rgb(255, 255, 255);
      font-size: 20px;
    }
  }
</style>
